/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MessageHandler;

import application_services.announcer;
import java.net.DatagramPacket;
import java.util.logging.Level;
import java.util.logging.Logger;
import postmaster.peer;
import postmaster.postmaster;

/**
 *
 * @author maniksin
 */
public class handler_thread extends Thread {
    
    private DatagramPacket dp;
    private peer p;
    private postmaster pm;
    
    
    public handler_thread(DatagramPacket dp, peer p, postmaster pm) {
        this.dp = dp;
        this.p = p;
        this.pm = pm;
        
    }
    
    private String[] list_commands() {
        
        String[] cmds = new String[submessage.values().length];
        int i = 0;
        for (submessage s : submessage.values()) {
            cmds[i] = s.cmd;
            i++;
        }
        
        return cmds;
    }
    
    @Override
    public void run() {
        
        messagehandler msg = messagehandler.decode_message(dp);
        
        messages msg_id = messages.PEER_REPLY;
        
        
        if (msg.msg_id == messages.USER_COMMAND) {
            // Received a command for self
            if (msg.sub_msg_id.supported_on_self == false) {
                Logger.global.log(Level.WARNING, "Postmaster received {0} for self!! Not handled.", msg.sub_msg_id.cmd);
                return;
            }
            
            msg_id = messages.USER_REPLY;
            
        }
        
        switch(msg.sub_msg_id) {
            case EXTERNAL_COMMAND:
                
                Logger.global.log(Level.INFO, "Executing command: {0}", msg.cmd_string[0]);
                String reply = "Not yet supporting " + msg.cmd_string[0];
                if (msg.type == message_type.ACKNOWLEDGE_ACTION) {
                    Logger.global.log(Level.INFO, "Sending command action reply to {0}", msg.source);
                    DatagramPacket reply_dp = messagehandler.create_reply(msg_id, dp, pm.conf.my_name, reply);
                    reply_dp.setAddress(dp.getAddress());
                    reply_dp.setPort(dp.getPort());
                    pm.send_message(reply_dp);
                }
                break;
                
            case LIST_COMMANDS:
                String[] cmd_list = list_commands();
                messagehandler reply_msg = new messagehandler();
                
                
                reply_msg.msg_id = msg_id;
                reply_msg.source = pm.conf.my_name;
                reply_msg.dest = msg.source;
                reply_msg.sub_msg_id = submessage.LIST_COMMANDS;
                reply_msg.type = message_type.ACKNOWLEDGE_ACTION;
                reply_msg.user_ip = msg.user_ip;
                reply_msg.user_port = msg.user_port;
                reply_msg.cmd_string = cmd_list;
                
                DatagramPacket reply_dp = messagehandler.create_message(reply_msg);
                reply_dp.setAddress(dp.getAddress());
                reply_dp.setPort(dp.getPort());
                pm.send_message(reply_dp);
                break;
                
                
                
            case ANNOUNCEMENT:
                // Expected string[] = <title> <message>
                
                announcer a = new announcer(msg.cmd_string[0], msg.cmd_string[1], p.get_name());
                break;
                
            default:
                Logger.global.log(Level.WARNING, "Unknown sub-command received from peer. Command not handled");
                break;
        }
        
        
        
    }
    
    
}
